package com.egg.ylt.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.StringSpanUtils;
import com.egg.ylt.pojo.CouponEntity;
import com.yonyou.framework.library.adapter.rv.CommonAdapter;
import com.yonyou.framework.library.adapter.rv.ViewHolder;
import com.yonyou.framework.library.common.utils.MoneyFormatUtil;

import java.math.BigDecimal;

/**
 * author:        yupeng
 * created on:    2019/3/1 14:45
 * description:   新优惠券列表
 */
public class ADA_ShopDetailCoupon extends CommonAdapter<CouponEntity.ListEntity> {

    public ADA_ShopDetailCoupon(Context context) {
        super(context);
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_shop_detail_coupon;
    }

    @Override
    protected void convert(ViewHolder holder, final CouponEntity.ListEntity listBean, final int position) {
        String str = new BigDecimal(listBean.getTicketAmount()).toPlainString();
        float scale = 0;
        if (str.length() > 4) {
            switch (str.length()) {
                case 5:
                    scale = 0.8f;
                    break;
                case 6:
                    scale = 0.7f;
                    break;
                case 7:
                    scale = 0.6f;
                    break;
                case 8:
                    scale = 0.5f;
                    break;

            }
        }

//        ((TextView) holder.getView(R.id.price_txt)).setText("￥" + new BigDecimal(listBean.getTicketAmount()).toPlainString());
        holder.setSpanText(R.id.price_txt,StringSpanUtils.init().addRelativeScale(scale, "￥").addRelativeScale(scale,str).build());
        holder.setText(R.id.coupon_name_txt, listBean.getTicketName());
        holder.setText(R.id.coupon_date_txt, listBean.getStartDate() + "至" + listBean.getEndDate());

        // 根据类型和状态设置优惠券样式
        setCouponBackground(holder, listBean.getTicketType(), listBean.getStatus());

        // 0-待使用，1-已使用 2-已过期 3-未领取
        switch (listBean.getStatus()) {
            case "3": // 未领取
                ((TextView) holder.getView(R.id.obtain_coupon_button)).setText("立即领取>>");
                holder.getView(R.id.obtain_coupon_button).setEnabled(true);
                holder.getView(R.id.ll_go_get).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onButtonClickListener != null) {
                            onButtonClickListener.onClick(listBean, position);
                        }
                    }
                });
                break;
            case "0": // 待使用
                ((TextView) holder.getView(R.id.obtain_coupon_button)).setText("已领取");
                holder.getView(R.id.obtain_coupon_button).setEnabled(false);
                break;

        }
    }

    private void setCouponBackground(ViewHolder holder, String type, String status) {
        switch (type) {
            case CouponEntity.TYPE_EXP:
                if ("3".equals(status)) {
                    holder.setBackgroundRes(R.id.rl_root, R.mipmap.bg_coupon_blue);
                } else if ("0".equals(status)) {
                    holder.setBackgroundRes(R.id.rl_root, R.mipmap.bg_coupon_blue_received);
                }
                break;
            case CouponEntity.TYPE_FILL_TO_DEDUCT:
                if ("3".equals(status)) {
                    holder.setBackgroundRes(R.id.rl_root, R.mipmap.bg_coupon_orange);
                } else if ("0".equals(status)) {
                    holder.setBackgroundRes(R.id.rl_root, R.mipmap.bg_coupon_orange_received);
                }
                break;
        }
    }

    private OnButtonClickListener onButtonClickListener;

    public interface OnButtonClickListener {
        void onClick(CouponEntity.ListEntity model, int position);
    }

    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }
}
